import Vue from 'vue'
import App from './App'
var WebIM = require("./utils/WebIM")["default"];

import cuCustom from './colorui/components/cu-custom.vue'
Vue.component('cu-custom', cuCustom)

import store from './store'
Vue.prototype.$store = store

import uView from 'uview-ui';
Vue.use(uView);


function toQueryPair(key, value) {
    if (typeof value == 'undefined') {
        return `&${key}=`;
    }
    return `&${key}=${value}`;
}
function objToParam(param) {
    if (Object.prototype.toString.call(param) !== '[object Object]') {
        return '';
    }
    let queryParam = '';
    for (let key in param) {
        if (param.hasOwnProperty(key)) {
            let value = param[key];
            queryParam += toQueryPair(key, value);
        }
    }
    queryParam = queryParam.substr(1, (queryParam.length - 1))
    console.log(queryParam)

    return queryParam;
}
import { user, broker } from "./utils/baseBottom";


Vue.mixin({
    methods: {
        jumpLink(link) {
            // link : {link: '', query: {}}
            var mix = [...user, ...broker]
            // console.log(mix)
            //link.needToken
			
			
			let token = uni.getStorageSync('token')
			if(token==""){
				uni.navigateTo({
					url:"/pages/index/index"
				})
			}
            if (link.needToken && !store.state.checkLogin) {
                var pages = getCurrentPages();
                if (pages[0].route == 'pagesA/my/index') {
                    return uni.showToast({
                        title: '请授权登录',
                        icon: 'none',
                        duration: 2000
                    });
                } else {
                    return uni.navigateTo({
                        url: '/pagesA/my/index'
                    });
                }
            }
            mix.forEach(v => {
                if (v.link == link.link) {
                    if (v.isSwitch) {
                        return uni.switchTab({
                            url: v.query ? v.link + '?' + objToParam(v.query) : v.link
                        })
                    } else {
	                        return uni.reLaunch({
                            url: v.query ? v.link + '?' + objToParam(v.query) : v.link
                        })
                    }

                }
            })
            uni.navigateTo({
                url: link.query ? link.link + '?' + objToParam(link.query) : link.link
            });
        },
		//选中组件的 选中后的回调函数
		        /**
		         * item: 选中后的返回对象 object
		         * nameNanme 全部的数据 string
		         * name: 选中后应该给谁赋值 string
		         * firstSP: 是否选中的第一个不限，让其他都不选中  Boolean
		         * isAlone: 是否是单选 Boolean
		         */
		        selectItem(item, nameName, name, firstSP, isAlone) {
		            console.log(item, nameName, name, firstSP, isAlone);
		            console.log(this[name]);
		            const index = this[name].indexOf(item.id);
		            if (index > -1) {
		                this[name].splice(index, 1);
		            } else {
		                if (firstSP) {
		                    if (this[nameName][0].id == item.id) {
		                        //如果选了第一个那直接全部只选中第一个
		                        this[name] = [this[nameName][0].id];
		                    } else if (
		                        this[name].indexOf(this[nameName][0].id) !== -1 &&
		                        item.id != this[nameName][0].id
		                    ) {
		                        //如果已经选中了第一w项不限，当前选中了其他那就取消选中不限
		                        this[name].push(item.id);
		                        let where0 = this[name].indexOf(this[nameName][0].id);
		                        this[name].splice(where0, 1);
		                    } else {
		                        this[name].push(item.id);
		                    }
		                } else {
		                    if (isAlone) {
		                        this[name] = [item.id];
		                    } else {
		                        this[name].push(item.id);
		                    }
		                }
		            }
		            console.log(this[name])
		        },
        //选中组件的 选中后的回调函数
        /**
         * item: 选中后的返回对象 object
         * nameNanme 全部的数据 string
         * name: 选中后应该给谁赋值 string
         * firstSP: 是否选中的第一个不限，让其他都不选中  Boolean
         * isAlone: 是否是单选 Boolean
         */
        selectItem(item, nameName, name, firstSP, isAlone) {
            console.log(item, nameName, name, firstSP, isAlone);
            console.log(this[name]);
            const index = this[name].indexOf(item.id);
            if (index > -1) {
                this[name].splice(index, 1);
            } else {
                if (firstSP) {
                    if (this[nameName][0].id == item.id) {
                        //如果选了第一个那直接全部只选中第一个
                        this[name] = [this[nameName][0].id];
                    } else if (
                        this[name].indexOf(this[nameName][0].id) !== -1 &&
                        item.id != this[nameName][0].id
                    ) {
                        //如果已经选中了第一w项不限，当前选中了其他那就取消选中不限
                        this[name].push(item.id);
                        let where0 = this[name].indexOf(this[nameName][0].id);
                        this[name].splice(where0, 1);
                    } else {
                        this[name].push(item.id);
                    }
                } else {
                    if (isAlone) {
                        this[name] = [item.id];
                    } else {
                        this[name].push(item.id);
                    }
                }
            }
            console.log(this[name])
        },
        setData: function (obj, callback) {
            let that = this;
            let keys = [];
            let val, data;
            Object.keys(obj).forEach(function (key) {
                console.log(that.$data)
                keys = key.split('.');
                val = obj[key];
                data = that.$data;
                keys.forEach(function (key2, index) {
                    if (index + 1 == keys.length) {
                        that.$set(data, key2, val);
                    } else {
                        if (!data[key2]) {
                            that.$set(data, key2, {});
                        }
                    }
                    data = data[key2];
                })
            });
            callback && callback();
        },
        //全局提示后的返回上一页面
        timeOutBack: function (time = 1500) {
            setTimeout(() => {
                uni.navigateBack({
                    delta: 1
                });
            }, time);
        },
        //查找一个数组中某个数据的某个字段
        takeSomeData(arr, id, name){
            let value = ''
            arr.forEach(v=>{
                if(v.id == id){
                    value = v[name]
                }
            })
            return value
        }
    }
})

// let imData = uni.getStorageSync('imData')
// if (imData) {
//     WebIM.conn.open({
//         apiUrl: WebIM.config.apiURL,
//         user: imData.user,
//         //   user: "opp123",
//         pwd: imData.pwd,
//         //   grant_type: "password",
//         appKey: WebIM.config.appkey
//     });
// }
// console.log(imData, 'imData')


Vue.config.productionTip = false

App.mpType = 'app'

const app = new Vue({
    ...App,
    store
})
app.$mount()
